#include <gtest/gtest.h>

//TODO: all move_syntax for resource management : memory && handle
//  QueryResult::move_syntax;  
//  Statement::move_syntax;

#include "helper/sqlite/MemoryDB.h"
#include "helper/sqlite/Statement.h"
TEST(CreateStatementTest, createStatement){
  helper::sqlite::MemoryDB db("hello");

  std::error_code ec;
  auto && stmt = db.createStatement("create table tab1 (one text primary key, two int);", ec);
  ASSERT_EQ(0, ec.value());
}


TEST(MemoryDBTest, use){
  // IDB && db = IDB::Memory(name)
  helper::sqlite::MemoryDB db("hello");
  ASSERT_FALSE(db.exists()); //opened -> exist ; non-opend -> not exist 

  ASSERT_EQ(0, db.open().value());
  ASSERT_TRUE(db.exists());

  ASSERT_EQ(0, db.close().value());
  ASSERT_EQ(0, db.destroy().value());
}


#include "helper/sqlite/FileDB.h"
TEST(FileDBTest , use){
  {
    helper::sqlite::FileDB db("hello", ".");

    ASSERT_FALSE(db.exists());
    ASSERT_EQ(0, db.open().value());
    ASSERT_TRUE(db.exists());
    ASSERT_EQ(0, db.close().value());
    ASSERT_EQ(0, db.destroy().value());
  }

  {// memory name for FileDB
    helper::sqlite::FileDB db("", ":memory:");
    ASSERT_FALSE(db.exists());
    ASSERT_EQ(std::errc::invalid_argument, db.open());
  }
}

#include "helper/sqlite/DB.h"

TEST(DBTest, use){
  {
    helper::sqlite::DB db("test_hello.db");
    ASSERT_FALSE(db.exists());
    ASSERT_EQ(0, db.open().value());
    ASSERT_TRUE(db.exists());
    ASSERT_EQ(0, db.close().value());
    ASSERT_EQ(0, db.destroy().value());
  }
  {

    helper::sqlite::MemoryDB db(":memory:");
    ASSERT_FALSE(db.exists());

    ASSERT_EQ(0, db.open().value());
    ASSERT_TRUE(db.exists());

    ASSERT_EQ(0, db.close().value());
    ASSERT_EQ(0, db.destroy().value());
  }

}


